import com.google.gson.Gson;
import org.gdal.gdal.Band;

import java.util.ArrayList;
import java.util.List;

public class ThreadUtil implements Runnable {

    private int i;
    private int iYSize;
    private int clipX;
    private int XS;
    private List<Band> bandList;
    private Double[] nodata;
    public Band desBand;

    private static Object obj = new Object();
    private static Object obj2 = new Object();

    //i:开始读取X数，xs:一次读取Y数，iYSize：y总数，clipX：x总数，nodata：没有数据的值，bandList：读取文件的list<Band>集合，desBand：写入文件Band。
    public ThreadUtil(int i, int XS, int iYSize, int clipX, Double[] nodata, List<Band> bandList, Band desBand) {
        this.i = i;
        this.XS = XS;
        this.iYSize = iYSize;
        this.clipX = clipX;
        this.bandList = bandList;
        this.nodata = nodata;
        this.desBand = desBand;
    }

    @Override
    public void run() {
        int ysize = XS;
        if (i > iYSize - XS) {
            ysize = iYSize % XS;
        }
        List<int[]> listarr = new ArrayList<int[]>();

        int[] cache = new int[clipX * ysize];
        synchronized (ThreadUtil.class) {
            bandList.get(0).ReadRaster(0, i, clipX, ysize, cache);
        }
//        for (int n = 0; n < bandList.size(); n += 2) {
//            int[] cache2 = new int[clipX * ysize];
//            synchronized (ThreadUtil.class) {
//                bandList.get(n).ReadRaster(0, i, clipX, ysize, cache);
//            }
//            listarr.add(n, cache);
//            if (n != bandList.size() - 1) {
//                synchronized (obj2) {
//                    bandList.get(n + 1).ReadRaster(0, i, clipX, ysize, cache2);
//                }
//                listarr.add(n + 1, cache2);
//            }
        //       }
//        int[] cache = new int[clipX * ysize];
//        for (int w = 0; w < clipX * ysize; w++) {
//            int[] carr = new int[listarr.size()];
//            for (int y = 0; y < listarr.size(); y++) {
//                carr[y] = listarr.get(y)[w];
//            }
//            int ca = 0;
//            boolean asd = false;
//            for (int z = 0; z < carr.length; z++) {
//                if (carr[z] != nodata[0]) {
//                    ca += carr[z];
//                } else {
//                    asd = true;
//                }
//            }
//            if (asd) {
//                cache[w] = nodata[0].intValue();
//            } else {
//                cache[w] = ca;
//            }
        //       }
        //从左上角开始，只写5行

        synchronized (obj) {
            desBand.WriteRaster(0, i, clipX, 1, cache);
            desBand.FlushCache();
        }
//
//        int[] buf = new int[clipX];
//        for (int i = 0;i < iYSize; i++) {
//            desBand.ReadRaster(0, i, clipX, 1, buf);    //读取一行数据
//
////            System.out.println("\n");
//            synchronized (obj) {
//                desBand.WriteRaster(0, i, clipX, iYSize, buf);
//                desBand.FlushCache();
//            }
//        }
    }

}
